﻿using Dapper;
using SIG.DAL.Dapper.Model;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIG.DAL.Dapper.Repositories
{
    public class FilterTemplateRepository
    {
        private DbConnection _connection;

        public FilterTemplate GetById(int id)
        {
            FilterTemplate result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Get<FilterTemplate>(id);
            }
            return result;
        }
        public int? Create(FilterTemplate model)
        {
            int? result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Insert(model);
            }
            return result;
        }

        public int Update(FilterTemplate model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Update(model);
            }
            return result;
        }
        public int Delete(FilterTemplate model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete(model);
            }
            return result;
        }

        public int Delete(int id)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete<FilterTemplate>(id);
            }
            return result;
        }

        public IEnumerable<FilterTemplate> GetActiveItems()
        {
            IEnumerable<FilterTemplate> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<FilterTemplate>(new { Active = true });
            }
            return result;
        }
        
       
        public IEnumerable<FilterTemplate> GetListPagedAcitveItems(int pageIndex, int pageSize, out int totalCount)
        {
            IEnumerable<FilterTemplate> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                totalCount = _connection.RecordCount<FilterTemplate>("where Active = 1");
                result = _connection.GetListPaged<FilterTemplate>(pageIndex, pageSize, "where Active=1", "CreatedDate desc");
            }
            return result;
        }
       
     
        public IEnumerable<FilterTemplate> GetListPagedItems(int pageIndex, int pageSize,  string keyword, out int totalCount)
        {
            IEnumerable<FilterTemplate> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                keyword = !string.IsNullOrEmpty(keyword) ? keyword.Replace("[", "[[]").Replace("%", "[%]") : keyword;
                string likeKeyword = "%" + keyword + "%";

                totalCount = _connection.RecordCount<FilterTemplate>("where Title like @Title",
                    new { Title = likeKeyword });
                result = _connection.GetListPaged<FilterTemplate>(pageIndex, pageSize,
                    "where Title like @Title", "CreatedDate desc",
                    new { Title = likeKeyword });
            }
            return result;
        }


        public int GetCountByParentId(int parentId)
        {

            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.RecordCount<FilterTemplate>("where CategoryId = @CategoryId",new { CategoryId=parentId});              
            }
            return result;
        }
    }

}
